<template>
<div>
  <!------------------------面包屑--------------------------->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{path:'/blank'}"><i class="el-icon-notebook-1"></i>考试管理</el-breadcrumb-item>
    <el-breadcrumb-item>题库管理</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <!------------------------搜索栏------------------------>
  <div style="text-align: left">
    <el-row style="margin-bottom: 10px" v-if="localUser.roleId==1||localUser.roleId==2">
        <el-select v-model="majorId" clearable placeholder="请选择专业" prefix-icon="el-icon-search" @visible-change="loadBankList" style="margin-right: 10px">
          <el-option v-for="item in majorList" :key="item.id" :label="item.majorName" :value="item.id"></el-option>
        </el-select>
      <el-button type="success" size="medium" icon="el-icon-search" @click="loadBankList">搜索</el-button>
    </el-row>
    <!------------------------添加题库------------------------>
    <el-row style="float: left;margin-bottom: 10px">
      <el-button type="primary" size="medium" icon="el-icon-plus" style="float: right;right: 10px" @click="$router.push('/bank/add-new')">添加题库</el-button>
    </el-row>

    <!------------------------题库表单--------------------------->
    <template>
      <el-table :data="BankData" stripe border style="width: 100%">
        <el-table-column prop="bankId" label="ID" width="60" align="center"></el-table-column>
        <el-table-column prop="bankId" label="题库名称" show-overflow-tooltip align="center">
          <template slot-scope="scope">
          <!-- --------------------------------------------跳转对应题库超链接-------------------------------------------- -->
            <a  style="text-decoration: none;color: #409EFF;cursor:pointer" @click="goToQuestion(scope.row.bankId)">{{ scope.row.bankName }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="majorName" show-overflow-tooltip header-align="center" align="center" width="150" label="专业名称"></el-table-column>
        <el-table-column prop="questionNum" show-overflow-tooltip align="center" width="80" label="题目数量"></el-table-column>
        <el-table-column prop="gmtCreate" show-overflow-tooltip  align="center" width="180" label="创建时间"></el-table-column>
        <el-table-column prop="gmtModified" show-overflow-tooltip  align="center" width="180" label="修改时间"></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" circle @click="openEditDialog(scope.row)"></el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" circle @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  ------------------------分页器------------------------  -->
      <div style="text-align: center; margin: 10px auto;float: right">
        <el-pagination @current-change="changePage" layout="total, prev, pager, next" :total="total" :current-page="currentPage" :page-size="pageSize"></el-pagination>
      </div>
    </template>
  </div>
  <!-- ------------------------Dialog 弹出对话框 嵌套表单版------------------------ -->
  <div>
    <el-dialog title="修改题库" :visible.sync="dialogFormVisible" width="480px">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px" style="margin: -10px 0 -28px 0">
        <el-form-item label="题库名称" prop="bankName">
          <el-input v-model="editForm.bankName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择专业" prop="majorId" style="text-align: left">
          <el-select v-model="editForm.majorId" placeholder="请选择专业" :disabled="isMajorId">
            <el-option v-for="item in majorList" :key="item.id" :label="item.majorName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin: 0">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      BankData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      rules: {
        bankName: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        majorId: [
          {required: true, message: '请选择专业', trigger: 'blur'},
        ]
      },
      //Dialog属性
      dialogFormVisible: false,
      isMajorId:false,
      majorList:[],
      editForm: {
        bankName: '',
        majorId: '',
      },
      majorId:'',
      localUser:{}

    }
  },
  methods:{
    /*跳转对应题库的试题管理*/
    goToQuestion(id){
      let path = '/question?bankId=' + id;
      this.$router.push(path);
    },
    openEditDialog(bank){
      let url = 'http://localhost:9282/bank/standard?bankId=' + bank.bankId;
      console.log('url =' + url);
      let localJwt = localStorage.getItem('localJwt');
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000){
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadBankList();
            }
          })
        }
      });
    },
    submitEditForm(){
      let url = 'http://localhost:9282/bank/update';
      console.log('url=' + url);

      let  formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);
      let localJwt = localStorage.getItem('localJwt');

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000){
          this.$message({
            message:'修改成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadBankList();
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else if(jsonResult == 40400){
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确定',
            callback:action => {// eslint-disable-line no-unused-vars
              this.dialogFormVisible = false;
              this.loadBankList();
            }
          });
        }else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确定',
            callback:action => {// eslint-disable-line no-unused-vars
            }
          });
        }
      });
    },
    openDeleteConfirm(bank){
      this.$confirm('此操作将永久删除【' + bank.bankId + '-' + bank.bankName + '】题库，是否继续？','提示',{
        confirmButtonText: '确认',
        cancelButtonText:'取消',
        type: "warning",
      }).then(() =>{
        this.handleDelete(bank);
      }).catch(() =>{
      });
    },
    handleDelete(bank) {
      let url = 'http://localhost:9282/bank/delete';
      console.log('url = ' + url);

      let formData = 'bankId=' + bank.bankId;
      console.log('formData = ' + formData);
      let localJwt = localStorage.getItem('localJwt');

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除相册成功！',
            type: 'success'
          });
          this.loadBankList();
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadBankList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          });
        }
      });
    },
    changePage(value){
      this.$router.replace('?page=' + value);
      this.loadBankList();
    },
    loadMajorList(){
      //http://localhost:9282/bank/listMajor
      let url = 'http://localhost:9282/bank/listMajor';
      let localJwt = localStorage.getItem('localJwt');
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000){
          this.majorList = jsonResult.data;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
              this.$router.back();
            }
          })
        }
      });
    },
    loadBankList(){
      let page = this.$router.currentRoute.query.page;
      if (!page){
        page = 1;
      }
      this.localUser = JSON.parse(localStorage.getItem("localUser"));
      let majorId = this.localUser.majorId;
      if(this.localUser.roleId==1||this.localUser.roleId==2){
        majorId = 0;
      }else{
        this.isMajorId = true;
      }
      if (this.majorId!=''){
        majorId = this.majorId;
      }

      let localJwt = localStorage.getItem('localJwt');
      let url = 'http://localhost:9282/bank/'+page+'/listMajor?majorId=' +majorId;
      console.log('localJwt =' + localJwt);
      console.log('url =' + url);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult.state);
        console.log(jsonResult.data.list);
        if (jsonResult.state == 20000){
          this.BankData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          })
        }
      });
    },
  },
  mounted() {
    this.loadBankList();
    this.loadMajorList();
  }
}
</script>

<style scoped>

</style>
